package com.csdj.controller;

import com.csdj.entity.AppInfo;

import com.csdj.service.IAppInfoService;
import com.csdj.service.DataDictionaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/app")
public class AppInfoController {
    private Logger log = Logger.getLogger("AppInfoController");

    @Resource
    IAppInfoService appInfoService;
    @Resource
    DataDictionaryService dataDictionaryService;
    /*@GetMapping("/gamelist")
    public String houLogin(Model model){
        List<AppInfo> gamelist= appInfoService.getallAPPInfo();
        model.addAttribute("gamelist",gamelist);
        return "developer/appinfolist";
    }*/

    @GetMapping("gamelist")
    public String allInfo(@RequestParam(required=true,defaultValue="1") Integer page, Model model) {
        PageHelper.startPage(page, 3);
        //查询
        List<AppInfo> appInfoList = appInfoService.getallAPPInfo();
        //创建PageInfo对象，保存查询出的结果，PageInfo是pageHelper中的对象
        PageInfo<AppInfo> p=new PageInfo<>(appInfoList);
        model.addAttribute("gamelist",appInfoList);
        model.addAttribute("p",p);
        return "developer/appinfolist";
    }

    @GetMapping("/gameall")
    public String gameall(){
        return "developer/appinfolist";
    }

    @GetMapping("/add")
    public String addAppInfo(Model model){
        model.addAttribute("pingtai",dataDictionaryService.comboBox("pingtai"));
        model.addAttribute("class1",dataDictionaryService.comboBox("class1"));
        model.addAttribute("class2",dataDictionaryService.comboBox("class2"));
        model.addAttribute("class3",dataDictionaryService.comboBox("class3"));
        return "developer/appinfoadd";
    }
    @PostMapping("/addinfoSuccess")
    public  String doadd(AppInfo appInfo,Model model){
        int result= appInfoService.insert(appInfo);
        if (result>0){
            model.addAttribute("name","添加成功");
        }else{
            model.addAttribute("name","添加失败");
        }
        return "developer/addinfoSuccess";
    }



    @GetMapping("/appinfomodify")
    public String updateAppInfo(Model model,int id){
        model.addAttribute("pingtai",dataDictionaryService.comboBox("pingtai"));
        model.addAttribute("class1",dataDictionaryService.comboBox("class1"));
        model.addAttribute("class2",dataDictionaryService.comboBox("class2"));
        model.addAttribute("class3",dataDictionaryService.comboBox("class3"));
        AppInfo appInfo = appInfoService.selectByPrimaryKey(id);
        System.out.println(appInfo.toString());
        model.addAttribute("appInfo",appInfo);
        return "developer/appinfomodify";
    }

    @PostMapping("/updateinfoSuccess")
    public String doupdate(AppInfo appInfo,Model model){
        int result= appInfoService.updateByPrimaryKeySelective(appInfo);
        if (result>0){
            model.addAttribute("name","修改成功");

        }else{
            model.addAttribute("name","修改失败");
        }
        return "developer/updateinfoSuccess";
    }
    @ResponseBody
    @PostMapping("delete")
    public String deleteapp(@RequestParam(value = "id",required = false) Integer id){
        log.info("delete() id="+id);
        int result= appInfoService.delete(id);
        if(result>0){
            return "true";
        }else{
            return "false";
        }

    }


}
